class UsersController < ApplicationController
	
	# Allow registration for all users without a login - of course!
	skip_before_filter :require_logged_in, only: [:new, :create]
	
	def index
		@users = User.paginate(page: params[:page])
	end
	
	def show
		@user = User.find(params[:id])
	end
	
	def new
		default_user_group_id = UserGroup.find_by_name('Users').id
		@user = User.new(user_group_id: default_user_group_id)
	end
	
	def create
		params[:user][:created_by] = File.basename(__FILE__)
		@user = User.create_with_default_permissions(params[:user])
		if @user.errors.any?
            @errors = @user.errors
			render 'new'
		else
			log_in @user
			redirect_to home_path, flash: { success: t('user.create.success') }
		end
	end
	
	def edit
		@user = User.find(params[:id])
	end
	
	def update
		@user = User.find(params[:id])
		params[:user][:updated_by] = current_user.id
		
		if @user.update_attributes(params[:user])
			redirect_to @user, flash: { success: t('user.update.success') }
		else
            @errors = @user.errors
			render 'edit'
		end
	end
	
	def destroy
		User.find(params[:id]).destroy
		redirect_to users_path, flash: { success: t('user.delete.success') }
	end
	
end
